/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sys.service;

import cn.hutool.core.util.ObjectUtil;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.scoreacademicyear.entity.ScoreAcademicYear;
import com.jeeplus.modules.scoreacademicyear.mapper.ScoreAcademicYearMapper;
import com.jeeplus.modules.sys.entity.*;
import com.alibaba.fastjson.JSON;
import com.jeeplus.modules.sys.mapper.StudentMoreduMainMapper;
import com.jeeplus.modules.sys.mapper.StudentMoreduViceAddMapper;
import com.jeeplus.modules.sys.mapper.StudentMoreduViceSubMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 学生德育档案Service
 * @author 于昭龙
 * @version 2022-02-11
 */
@Service
@Transactional(readOnly = true)
public class StudentMoreduMainService extends CrudService<StudentMoreduMainMapper, StudentMoreduMain> {

	@Autowired
	private StudentMoreduViceAddMapper studentMoreduViceAddMapper;
	@Autowired
	private StudentMoreduViceSubMapper studentMoreduViceSubMapper;
	@Autowired
	private ScoreAcademicYearMapper scoreAcademicYearMapper;
	@Autowired
	@Qualifier("longRedisTemplate")
	private RedisTemplate<String,Object> redisTemplateObject;

	public StudentMoreduMain get(String id, String time) {
		StudentMoreduMain studentMoreduMain = super.get(id);
		if (studentMoreduMain != null){
			StudentMoreduViceAdd studentMoreduViceAdd = new StudentMoreduViceAdd(studentMoreduMain);
			studentMoreduViceAdd.setTime(time);
			studentMoreduMain.setStudentMoreduViceAddList(studentMoreduViceAddMapper.findList(studentMoreduViceAdd));
			StudentMoreduViceSub studentMoreduViceSub = new StudentMoreduViceSub(studentMoreduMain);
			studentMoreduViceSub.setTime(time);
			studentMoreduMain.setStudentMoreduViceSubList(studentMoreduViceSubMapper.findList(studentMoreduViceSub));
		}
		return studentMoreduMain;
	}
	 public int countPoint(String classes) {
		return mapper.countPoint(classes);
	 }
	public StudentMoreduMain getid(String id) {
		return mapper.getid(id);
	}
	public List<StudentMoreduMain> findList(StudentMoreduMain studentMoreduMain) {
		String queryName = this.getClass().getSimpleName()+".findList"+ studentMoreduMain.toString();
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<StudentMoreduMain> data;
		// 如果缓存中有则直接返回
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,StudentMoreduMain.class);
			logger.info("走了byReason缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = super.findList(studentMoreduMain);
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName, JSON.toJSONString(data), 2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}

    public  List<StudentMoreduMain>	downlist(StudentMoreduMain studentMoreduMain) {
		String queryName = this.getClass().getSimpleName()+".downlist"+ studentMoreduMain.toString();
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<StudentMoreduMain> data;
		// 如果缓存中有则直接返回
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,StudentMoreduMain.class);
			logger.info("走了byReason缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.downlist(studentMoreduMain);
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName, JSON.toJSONString(data), 2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}
	public  List<String> gradelist(String  yuan) {
		String queryName = this.getClass().getSimpleName()+".gradelist"+ yuan;
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);

		List<String> data;
		// 如果缓存中有则直接返回
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,String.class);
			logger.info("走了byReason缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.gradelist(yuan);
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName, JSON.toJSONString(data), 2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}
	public Page<StudentMoreduMain> findPage(Page<StudentMoreduMain> page, StudentMoreduMain studentMoreduMain) {
		dataRuleFilter(studentMoreduMain);
		studentMoreduMain.setPage(page);
		String queryName = this.getClass().getSimpleName()+".findPage"+studentMoreduMain.toString() + page.getPageNo() + page.getPageSize();
		Page<StudentMoreduMain> tempPage;
		// 先去查询缓存
		tempPage = (Page<StudentMoreduMain>)redisTemplateObject.opsForValue().get(queryName);
		// 如果缓存中有则直接返回
		if(Objects.nonNull(tempPage)){
			page = tempPage;
			System.out.println("我有数据size");
		}else {
			//缓存中没有则从数据库中查询
			page.setList(mapper.findList(studentMoreduMain));
			redisTemplateObject.opsForValue().set(queryName,page,2, TimeUnit.DAYS);
		}
		return page;
	}
	public List<PinYin1> queryName(){
		String queryName = this.getClass().getSimpleName()+".queryName";
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<PinYin1> data;
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,PinYin1.class);
			logger.info("走了queryName缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.queryName();
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName,JSON.toJSONString(data),2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}
	public List<PinYin1> queryInstitute(){
		String queryName = this.getClass().getSimpleName()+".queryInstitute";
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<PinYin1> data;
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,PinYin1.class);
			logger.info("走了queryInstitute");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.queryInstitute();
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName,JSON.toJSONString(data),2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}
	public List<PinYin1> queryTeacher(){
		String queryName = this.getClass().getSimpleName()+".queryTeacher";
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<PinYin1> data;
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,PinYin1.class);
			logger.info("走了queryTeacher缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.queryTeacher();
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName,JSON.toJSONString(data),2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;

	}

	public Page<StudentMoreduMain> sort(Page<StudentMoreduMain> page, StudentMoreduMain studentMoreduMain) {
		dataRuleFilter(studentMoreduMain);
		studentMoreduMain.setPage(page);
		String queryName ="StudentMoreduMain.sort"+studentMoreduMain.toString() + page.getPageNo() + page.getPageSize();
		Page<StudentMoreduMain> tempPage;
		// 先去查询缓存
		tempPage = (Page<StudentMoreduMain>)redisTemplateObject.opsForValue().get(queryName);
		// 如果缓存中有则直接返回
		if(Objects.nonNull(tempPage)){
			page = tempPage;
		}else {
			//缓存中没有则从数据库中查询
			page.setList(mapper.sort(studentMoreduMain));
			redisTemplateObject.opsForValue().set(queryName,page,2, TimeUnit.DAYS);
		}
		return page;
	}
	public Page<StudentAddList> queryDetail(Page<StudentAddList> page, StudentAddList studentMoreduMain) {

		dataRuleFilter(studentMoreduMain);
		studentMoreduMain.setPage(page);
		String queryName ="StudentMoreduMain.queryDetail"+studentMoreduMain.toString() + page.getPageNo() + page.getPageSize();
		Page<StudentAddList> tempPage;
		// 先去查询缓存
		tempPage = (Page<StudentAddList>)redisTemplateObject.opsForValue().get(queryName);
		// 如果缓存中有则直接返回
		if(Objects.nonNull(tempPage)){
			page = tempPage;
			System.out.println("我有数据size");
		}else {
			//缓存中没有则从数据库中查询
			page.setList(mapper.queryDetail(studentMoreduMain));
			redisTemplateObject.opsForValue().set(queryName,page,2, TimeUnit.DAYS);
			logger.info("放入byReason缓存");
		}
		return page;
	}

	public StudentMoreduMain exportByIndividualAttribute(StudentMoreduMain studentMoreduMain) {
		StudentMoreduMain studentMoreduMain1 = mapper.exportByIndividualAttribute(studentMoreduMain);
		ScoreAcademicYear scoreAcademicYear = scoreAcademicYearMapper.findScoreByYear(studentMoreduMain);
		System.out.println((int)Double.parseDouble(scoreAcademicYear.getScore()) + "  123");
		System.out.println(studentMoreduMain1.getCurrentMoraleduPoint());
		studentMoreduMain1.setCurrentMoraleduPoint(studentMoreduMain1.getCurrentMoraleduPoint() + (int)Double.parseDouble(scoreAcademicYear.getScore()) );
		return studentMoreduMain1;
	}

	public List<addPoint> exportByIndividualAttributeAdd(StudentMoreduMain studentMoreduMain) {
		return mapper.exportByIndividualAttributeAdd(studentMoreduMain);
	}

	public List<subPoint> exportByIndividualAttributeSub(StudentMoreduMain studentMoreduMain) {
		return mapper.exportByIndividualAttributeSub(studentMoreduMain);
	}

	public Page<StudentMoreduMain> queryNot(Page<StudentMoreduMain> page, StudentMoreduMain studentMoreduMain) {
		dataRuleFilter(studentMoreduMain);
		studentMoreduMain.setPage(page);
		String queryName ="StudentMoreduMain.queryNot"+studentMoreduMain.toString() + page.getPageNo() + page.getPageSize();
		Page<StudentMoreduMain> tempPage;
		// 先去查询缓存
		tempPage = (Page<StudentMoreduMain>)redisTemplateObject.opsForValue().get(queryName);
		// 如果缓存中有则直接返回
		if(Objects.nonNull(tempPage)){
			page = tempPage;
		}else {
			//缓存中没有则从数据库中查询
			page.setList(mapper.queryNot(studentMoreduMain));
			redisTemplateObject.opsForValue().set(queryName,page,2, TimeUnit.DAYS);
		}
		return page;
	}

	public Page<StudentMoreduMain> queryAbove(Page<StudentMoreduMain> page, StudentMoreduMain studentMoreduMain) {
		dataRuleFilter(studentMoreduMain);
		studentMoreduMain.setPage(page);
		String queryName ="StudentMoreduMain.queryAbove"+studentMoreduMain.toString() + page.getPageNo() + page.getPageSize();
		Page<StudentMoreduMain> tempPage;
		// 先去查询缓存
		tempPage = (Page<StudentMoreduMain>)redisTemplateObject.opsForValue().get(queryName);
		// 如果缓存中有则直接返回
		if(Objects.nonNull(tempPage)){
			page = tempPage;
		}else {
			//缓存中没有则从数据库中查询
			page.setList(mapper.queryAbove(studentMoreduMain));
			redisTemplateObject.opsForValue().set(queryName,page,2, TimeUnit.DAYS);
		}
		return page;
	}

	@Transactional(readOnly = false)
	public void save(StudentMoreduMain studentMoreduMain) {
		super.save(studentMoreduMain);
		for (StudentMoreduViceAdd studentMoreduViceAdd : studentMoreduMain.getStudentMoreduViceAddList()){
			if (studentMoreduViceAdd.getId() == null){
				continue;
			}
			if (StudentMoreduViceAdd.DEL_FLAG_NORMAL.equals(studentMoreduViceAdd.getDelFlag())){
				if (StringUtils.isBlank(studentMoreduViceAdd.getId())){
					studentMoreduViceAdd.setAdd(studentMoreduMain);
					studentMoreduViceAdd.preInsert();
					studentMoreduViceAddMapper.insert(studentMoreduViceAdd);
				}else{
					studentMoreduViceAdd.preUpdate();
					studentMoreduViceAddMapper.update(studentMoreduViceAdd);
				}
			}else{
				studentMoreduViceAddMapper.delete(studentMoreduViceAdd);
			}
		}
		for (StudentMoreduViceSub studentMoreduViceSub : studentMoreduMain.getStudentMoreduViceSubList()){
			if (studentMoreduViceSub.getId() == null){
				continue;
			}
			if (StudentMoreduViceSub.DEL_FLAG_NORMAL.equals(studentMoreduViceSub.getDelFlag())){
				if (StringUtils.isBlank(studentMoreduViceSub.getId())){
					studentMoreduViceSub.setSub(studentMoreduMain);
					studentMoreduViceSub.preInsert();
					studentMoreduViceSub.setSubState(studentMoreduViceSub.getSubPoint());
					studentMoreduViceSubMapper.insert(studentMoreduViceSub); //插入附表数据
					Integer childrenPoint = Integer.parseInt(studentMoreduViceSub.getSubPoint()); //获取附表减分数
					Integer mainPoint = studentMoreduMain.getCurrentMoraleduPoint();  //转型成integer相减
					studentMoreduMain.setCurrentMoraleduPoint(mainPoint - childrenPoint);
					super.save(studentMoreduMain);
				}else{
					studentMoreduViceSub.preUpdate();
					Integer subPoint = Integer.parseInt(studentMoreduViceSub.getSubPoint()); //减分分数
					Integer presubPoint = Integer.parseInt(studentMoreduViceSub.getSubState()); //上次减分分数
					Integer mainPoint = studentMoreduMain.getCurrentMoraleduPoint(); //主表德育分
					studentMoreduMain.setCurrentMoraleduPoint(mainPoint + (presubPoint - subPoint)); //计算德育分
					super.save(studentMoreduMain); //保存到主表
					studentMoreduViceSub.setSubState(studentMoreduViceSub.getSubPoint()); //修改状态
					studentMoreduViceSubMapper.update(studentMoreduViceSub); //修改
				}
			}else{
				studentMoreduViceSubMapper.delete(studentMoreduViceSub);
				Integer subPoint = Integer.parseInt(studentMoreduViceSub.getSubPoint());  //减分分数
				Integer mainPoint = studentMoreduMain.getCurrentMoraleduPoint(); //主表德育分
				studentMoreduMain.setCurrentMoraleduPoint(subPoint + mainPoint); //计算德育分
				super.save(studentMoreduMain);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(StudentMoreduMain studentMoreduMain) {
		deleteCache();
		super.delete(studentMoreduMain);
		studentMoreduViceAddMapper.delete(new StudentMoreduViceAdd(studentMoreduMain));
		studentMoreduViceSubMapper.delete(new StudentMoreduViceSub(studentMoreduMain));
	}

	public void deleteCache () {
		String keyPre = "StudentMoreduMain";
		Set keys = redisTemplateObject.keys(keyPre + "*");
		if (ObjectUtil.isNotEmpty(keys)) {
			redisTemplateObject.delete(keys);
		}
	}
}